package com.epalmpay.controller.channel.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.activity.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.activity.ActivityUnroutineInDTO;
import com.epalmpay.dto.activity.RedpackageCouponOutDTO;
import com.epalmpay.dto.activity.RedpackageCouponQueryDTO;
import com.epalmpay.dto.activity.UnroutineInDTO;
import com.epalmpay.dto.activity.UnroutineListOutDTO;
import com.epalmpay.dto.activity.UnroutineOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.service.activity.IActivityUnroutineService;
import com.epalmpay.util.Tools;

@Controller
@RequestMapping(value="/channel/activity")
public class UnroutineController extends BaseController {
     
	@Resource
	private IActivityUnroutineService activityUnroutineService;
	/**
	 * 显示动态活动分页列表
	 * 
	 */
	@RequestMapping(value="/unroutineList")
	@ResponseBody
	public UnroutineListOutDTO  unroutineList(UnroutineInDTO unroutineInDTO,PageDTO pageDTO){
		UnroutineListOutDTO unroutineListOutDTO=new UnroutineListOutDTO();
		try {
			Page page=Tools.pageEmtity(pageDTO);
			
            //查询时取数据			
			if(unroutineInDTO.getIsReload()){
				//获取动态活动列表信息
				List<UnroutineOutDTO> unroutineOutDTOList = activityUnroutineService.
						getUnroutineList(unroutineInDTO, page);
				unroutineListOutDTO.setUnroutineOutDTOList(unroutineOutDTOList);
			}
			unroutineListOutDTO.setPage(page); 
			unroutineListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return unroutineListOutDTO;
		} catch (ApplicationException ae) { 
			unroutineListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		 catch (Exception e) { 
			 unroutineListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			 e.printStackTrace();
			 logger.error(e);
		}
		return unroutineListOutDTO;
	}

	/*
	*  新增动态活动
	* */
	@RequestMapping("/addUnroutine")
	@ResponseBody
	public BaseOutDTO addUnroutine(@RequestBody ActivityUnroutineInDTO activityUnroutineInDTO) {
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {
			return activityUnroutineService.addUnroutine(activityUnroutineInDTO);
		} catch (Exception e) {
			e.printStackTrace();
			baseOutDTO.setErrorMsg(e.getMessage());
		}
		baseOutDTO.setErrorCode(Rescode.FAIL);
		return baseOutDTO;
	}

	/*
	*  增加活动预算
	* */
	@RequestMapping("/changeMaxAmount")
	@ResponseBody
	public BaseOutDTO changeMaxAmount(@RequestBody Map<String, Integer> param) {
		return activityUnroutineService.changeMaxAmount(param);
	}

	/*
	*  活动详情
	* */
	@RequestMapping("/getUnroutineActivity")
	@ResponseBody
	public ActivityUnroutineOutDTO getUnroutineActivity(@RequestBody Long id) {
		return activityUnroutineService.getUnroutineActivity(id);
	}


//	@RequestMapping("/getUnroutineActivity")
//	@ResponseBody
//	public ActivityUnroutineOutDTO getUnroutineActivity(@RequestBody ActivityUnroutineInDTO activityUnroutineInDTO) {
//		return activityUnroutineService.getUnroutineActivity(activityUnroutineInDTO);
//	}


	/*
	*  活动参与情况
	* */
	@RequestMapping("/listUnroutineParticipation")
	@ResponseBody
	public ActivityParticipationOutListDTO listUnroutineParticipation(@RequestBody ActivityParticipationQueryDTO activityParticipationQueryDTO) {
		return activityUnroutineService.listPageParticipation(activityParticipationQueryDTO);
	}


	/*
	*  活动参与详情
	* */
	@RequestMapping("/activityParticipationDetail")
	@ResponseBody
	public ActivityParticipationDetailOutDTO activityParticipationDetail(@RequestBody Map<String, Object> map) {
		Long id = (long)(Integer) map.get("id");
		Integer sendType = (Integer) map.get("sendType");
		return activityUnroutineService.activityParticipationDetail(id, sendType);
	}


	/*
	*  结束活动
	* */
	@RequestMapping("/overUnroutineActivity")
	@ResponseBody
	public BaseOutDTO overUnroutineActivity(@RequestBody ActivityUnroutineInDTO activityUnroutineInDTO) {
		return activityUnroutineService.overUnroutineActivity(activityUnroutineInDTO);
	}



	@RequestMapping("/redpackageCoupon")
	@ResponseBody
	public List<RedpackageCouponOutDTO> redpackageCoupon(@RequestBody RedpackageCouponQueryDTO redpackageCouponQueryDTO) {
		try {
			return activityUnroutineService.listRedpackageCoupon(redpackageCouponQueryDTO);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			logger.error("get redpackage coupon list error");
			return new ArrayList<>();
		}
	}

	/*
	*  弹出框活动选择列表
	* */
	@RequestMapping("/activitySelective")
	@ResponseBody
	public ActivitySelectiveListOutDTO activitySelective(@RequestBody ActivitySelectiveQueryDTO activitySelectiveQueryDTO) {
		return activityUnroutineService.activitySelective(activitySelectiveQueryDTO);
	}
}
